<?php

namespace app\admin\controller;

use app\admin\business\AdminUser;
use app\BaseController;
use app\common\lib\Status;
use think\facade\View;
use app\admin\controller\AdminBase;

class Login extends AdminBase
{
    // 判断用户是否登录过，登录过则跳到主页。
    public function initialize()
    {
        if ($this->isLogin()) {
            return $this->redirect((string) url('index/index'));
        }
    }

    public function index()
    {
        return View::fetch();
    }

    public function md5()
    {
        session('admin_user', null);
        if (session('?admin_user')) {
            echo session('admin_user.password');
        } else {
            echo '213';
        }
    }

    // 登录验证！
    public function check()
    {
        $input = [
            'username' => input('post.username'),
            'password' => input('post.password'),
            'captcha' => input('post.captcha')
        ];
        // return json($input);
        try {
            $adminUser = new AdminUser();
            $result = $adminUser->getAdminUserByUserInfo($input);
        } catch (\Exception $e) {
            return Status::show(config('status.error'), $e->getMessage());
        }
        // return json($result);
        return Status::show(config('status.success'), '登录成功！');
    }
}
